﻿using Common;
using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using NB.MGJ.Model.AntiFake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.AntiFake
{
    public class AntiFake_PageCreateService
    {




        public BaseRepository _baseRepository = new BaseRepository();

        #region 增

        public int PageCreateAddService(AntiFake_PageCreate model)
        {
            int rulset = _baseRepository.InsertReturnID<AntiFake_PageCreate>(model);
            return rulset;
        }


        #endregion


        #region 删

        public int PageCreateDelete(int id)
        {
            string sql = "update antifake_pagecreate set ViewDel=1 where ID=" + id;
            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }



        #endregion


        #region 改
        public int PageCreateUpdateService(AntiFake_PageCreate model)
        {
            int rulset = _baseRepository.Update<AntiFake_PageCreate>(model);
            return rulset;
        }



        #endregion


        #region 查


        public AntiFake_PageCreate GetModelByID(int id)
        {
            var sql = "select * from antifake_pagecreate where ID=" + id;
            var model = _baseRepository.ExecuteSqlCollection<AntiFake_PageCreate>(sql).ToList().FirstOrDefault();
            return model;
        }

        public List<AntiFake_PageCreate> GetAllListByEID(int Eid)
        {
            string sql = "select * from AntiFake_PageCreate where EnterpriseId=" + Eid;
            IEnumerable<AntiFake_PageCreate> _query = _baseRepository.ExecuteSqlCollection<AntiFake_PageCreate>(sql);
            return _query.ToList();
        }

        public PagedList<AntiFake_PageCreate> GetPageListByEid(ref PagerModel pager, int Eid)
        {
            pager.FldName = @"*";
            pager.TblName = @"(select * from antifake_pagecreate where EnterpriseId=" + Eid + " and ViewDel=0 ) s";

            if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == " ")
                pager.FldSort = " s.IsDefault desc ";
            MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);
            IEnumerable<AntiFake_PageCreate> resultList = _baseRepository.ExecuteStoredProcedureCollection<AntiFake_PageCreate>("Common_PageList", parameters);
            return new PagedList<AntiFake_PageCreate>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
        }


        //获取机构默认防伪页面
        public AntiFake_PageCreate GetEnterpriseDefaultAntiFakePage(int EID)
        {
            string sql = " SELECT * from antifake_pagecreate where  IsDefault=(select Max(IsDefault) from antifake_pagecreate where EnterpriseId=" + EID + " and ViewDel=0 ) and EnterpriseId="+EID+"  and ViewDel=0";
            var pageModel = _baseRepository.ExecuteSqlCollection<AntiFake_PageCreate>(sql).ToList().FirstOrDefault();
            return pageModel;
        }

        public AntiFake_PageCreate CreatePageModel(int EID,int templetId)
        {

            if (templetId == 0 || templetId == 1)
            {
                var pageModel = new AntiFake_PageCreate();
                pageModel.EnterpriseId = EID;
                pageModel.Title = "品牌商品验证中心";
                pageModel.TimeOptions = 3;
                pageModel.PicShowType = 1;

                //pageModel.LbtJson = "[\"http://avrilbucket.oss-cn-beijing.aliyuncs.com/93e2880e-155a-4d04-bcff-2da6b0aa0163msla2-750x400px.jpg\"]";

                pageModel.BackgroundImg = "http://shnbbucket.oss-cn-beijing.aliyuncs.com/11915eba-dd61-46b8-ba3e-f2370ebf03ab11.jpg";

                pageModel.MainContent=@"<p style='text-align:center'><img src='http://shnbbucket.oss-cn-beijing.aliyuncs.com/f1212133-feb9-42d0-a48e-f7eb9ca7ff56微页面-公众号二维码_03.png' title='微页面-公众号二维码_03.png' alt='微页面-公众号二维码_03.png' width='201' height='94'/></p>'";

                pageModel.DragImgheight = "20%";
                pageModel.DragImgwidth = "38%";
                pageModel.DragImgtop = "10%";
                pageModel.DragImgleft = "30%";
                pageModel.DragImgSrc = "http://shnbbucket.oss-cn-beijing.aliyuncs.com/fe6c2733-86ac-42eb-8124-fa006f0af1df12.png";
                pageModel.ChangeDate = DateTime.Now;
                pageModel.ViewDel = 0;
                pageModel.IsDefault = 0;
                pageModel.TempletId=templetId;
                return pageModel;

            }

             else  if (templetId == 2)
            {
                var pageModel = new AntiFake_PageCreate();
                pageModel.EnterpriseId = EID;
                pageModel.Title = "品牌商品验证中心";
                pageModel.TimeOptions = 3;
                pageModel.PicShowType = 0;

                pageModel.LbtJson = "[\"http://avrilbucket.oss-cn-beijing.aliyuncs.com/93e2880e-155a-4d04-bcff-2da6b0aa0163msla2-750x400px.jpg\"]";

                pageModel.BackgroundImg = "";

                pageModel.DragImgheight = "6.51%";
                pageModel.DragImgwidth = "18.52%";
                pageModel.DragImgtop = "10%";
                pageModel.DragImgleft = "10%";
                pageModel.DragImgSrc = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/topImg.png";
                pageModel.ChangeDate = DateTime.Now;
                pageModel.ViewDel = 0;
                pageModel.IsDefault = 0;
                pageModel.TempletId=templetId;
                return pageModel;
            }
            else
            {
                var pageModel = new AntiFake_PageCreate();
                pageModel.EnterpriseId = EID;
                pageModel.Title = "品牌商品验证中心";
                pageModel.TimeOptions = 3;
                pageModel.PicShowType = 0;

                pageModel.LbtJson = "[\"http://avrilbucket.oss-cn-beijing.aliyuncs.com/93e2880e-155a-4d04-bcff-2da6b0aa0163msla2-750x400px.jpg\"]";

                pageModel.BackgroundImg = "";

                pageModel.DragImgheight = "6.51%";
                pageModel.DragImgwidth = "18.52%";
                pageModel.DragImgtop = "10%";
                pageModel.DragImgleft = "10%";
                pageModel.DragImgSrc = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/topImg.png";
                pageModel.ChangeDate = DateTime.Now;
                pageModel.ViewDel = 0;
                pageModel.IsDefault = 0;
                pageModel.TempletId=templetId;
                return pageModel;

            }

            
         
        }









        #endregion




        #region 其他

        public int SetDefault(int Id)
        {
            var sql =" select (select  MAX(IsDefault) from  antifake_pagecreate where EnterpriseId=(select EnterpriseId from antifake_pagecreate where ID="+Id+")) as max";
            var dt = _baseRepository.GetDataTable(sql);
            int max = Convert.ToInt32(dt.Rows[0]["max"]);
            max++;
            var sql2 = "update antifake_pagecreate set IsDefault=" + max + " where ID="+Id;
            var result = _baseRepository.ExecuteSql<int>(sql2);
            return result;
        }


        #endregion






        #region  局部修改

        //修改轮播图图片
        public int UpdateLunBoTu(int TimeOptions, string imgListJson, int Id)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("update antifake_pagecreate set TimeOptions=" + TimeOptions + " ,LbtJson='" + imgListJson + "'  where ID=" + Id);
            var sql = sb.ToString();
            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }

        public int UpdateGroupImg(GroupImg model)
        {
            StringBuilder sb=new StringBuilder();
               sb.Append("update antifake_pagecreate set");

               sb.Append(" TimeOptions="+model.TimeOptions);
               sb.Append(",LbtJson='"+model.ImgListJson+"'");
               sb.Append(",PicShowType="+model.PicShowType);

               sb.Append(",DragImgheight='"+model.DragModel.DragImgheight+"'");
               sb.Append(",DragImgwidth='"+model.DragModel.DragImgwidth+"'");
               sb.Append(",DragImgtop='"+model.DragModel.DragImgtop+"'");
               sb.Append(",DragImgleft='"+model.DragModel.DragImgleft+"'");
               sb.Append(",DragImgSrc='"+model.DragModel.DragImgSrc+"'");

               sb.Append(" where ID="+model.Id);
  
             var sql=sb.ToString();
             var result=_baseRepository.ExecuteSql<int>(sql);

             return result;
        }


        public int UpdateLunBoTuAndDrag(int TimeOptions, string imgListJson, int picShowType, string dragImgSrc, int Id = 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update antifake_pagecreate set TimeOptions=" + TimeOptions + " ,LbtJson='" + imgListJson + " ,PicShowType='" + picShowType + " ,LbtJson='" + dragImgSrc + "  where ID=" + Id);

            return 1;
        }

        //修改背景图
        public int UpdateBackGroundImg(string bimg, int Id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update antifake_pagecreate set BackgroundImg='" + bimg + "' where ID=" + Id);
            var sql = sb.ToString();

            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }


        //修改标题
        public int UpdateTitle(string title, int Id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update antifake_pagecreate set Title='" + title + "' where ID=" + Id);
            var sql = sb.ToString();
            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }

        public int RichTextSave(string mainContent,int Id)
        {
            string sql = "update antifake_pagecreate set MainContent='" + mainContent + "' where ID="+Id;
            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }


        #endregion



        public GenCodeModel GetCodeModelByCode(string code)
        {
            string type=CheckCodeType(code);

            var tableNameParm1 = "nbcodemain.antifake_digitcode";
            var tableNameParm2 = code.Substring(0, 6);
            var source = tableNameParm1 + tableNameParm2;

            var sql="select * from "+source+" where "+type+" = "+code;
        
            var result=_baseRepository.ExecuteSqlCollection<GenCodeModel>(sql).FirstOrDefault();

            return result;
        }






        #region 验证码的类型

        public string CheckCodeType(string code)
        {

            var type = string.Empty;
            var codeLength = code.Length;

            if (codeLength >= 16 && codeLength <= 18)
            {
                type = "Wl_Code";
            }
            else if (codeLength >= 19 && codeLength <= 24)
            {
                var tableNameParm1 = "antifake_digitcode";
                var tableNameParm2 = code.Substring(0, 6);
                var source = tableNameParm1 + tableNameParm2;
                string sql = "SELECT table_name FROM information_schema.TABLES WHERE table_name ='" + source + "'";
                var tableName = _baseRepository.GetSingle(sql);

                if (tableName == null)
                {
                    type = "Fw_CodeSecret";
                }
                else
                {
                    type = "Fw_Code";
                }
            }
            else
            {
                type = "error";
            }
            return type;
        }

        //根据码去获取机构ID
        public int GetEIDByCode(string code)
        {
            string type = CheckCodeType(code);
            var subCode = "";
            var ECode = "";
            int EID = 0;
            if (type == "Fw_CodeSecret")
            {
                subCode = MiMa.Jiemi(code, AppConst.DigitCodeKey);

            }
            else
            {
                subCode = code;
            }
            ECode = subCode.Substring(0, 4);
            EID = Convert.ToInt32(ECode.TrimStart(new char[] { '0' }));
            return EID;
        }






        #endregion


        #region  未知
        public int GetCreatePageId(int eid)
        {
            string sql = string.Format(" select count(*) from AntiFake_PageCreate where EnterpriseId=" + eid);
            return Convert.ToInt32(_baseRepository.GetSingle(sql));
        }



        public List<AntiFake_Digitcode> GetCodeBySearchService(string code, int EnterpriseId)
        {
            string codesearch = string.Empty;
            codesearch = MiMa.Jiemi(code, AppConst.DigitCodeKey);
            codesearch = codesearch.Substring(0, 6);
            string sql = "select*from nbcodemain.antifake_digitcode" + codesearch + " a LEFT JOIN nbsecuritymain.organization_enterprise b on a.EnterpriseId=b.ID where EnterpriseId=" + EnterpriseId + " and Fw_CodeSecret=" + code + "";

            IEnumerable<AntiFake_Digitcode> _query = _baseRepository.ExecuteSqlCollection<AntiFake_Digitcode>(sql);
            return _query.ToList();
        }


        #endregion








    }



}







